<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关键词" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入关键词"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="国家" prop="country" v-if="isCountryshow">
        <el-select v-model="queryParams.country" placeholder="请选择">
          <el-option
            v-for="dict in countryTypeOptions"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否原创文章" prop="isYc"  v-if="isYcshow">
        <el-select v-model="queryParams.isYc" placeholder="请选择">
          <el-option
            v-for="dict in dict.type.sys_is_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否推荐" prop="isTj"  v-if="isTjshow">
        <el-select v-model="queryParams.isTj" placeholder="请选择">
          <el-option
            v-for="dict in dict.type.sys_is_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否精选" prop="isRm"  v-if="isRmshow">
        <el-select v-model="queryParams.isRm" placeholder="请选择">
          <el-option
            v-for="dict in dict.type.sys_is_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="多媒体类型" prop="dmtType"  v-if="dptlxshow" >
        <el-select v-model="queryParams.dmtType" placeholder="请选择">
          <el-option
            v-for="dict in dict.type.dmt_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分类" prop="flType"  v-if="flTypeshow">
        <el-select v-model="queryParams.flType" placeholder="请选择分类">
          <el-option
            v-for="dict in articleTypeOptions"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产品分类" prop="cpType"  v-if="cpTypeshow">
        <div class="treeselect" style="width: 300px">
          <treeselect v-model="queryParams.cpType" :options="cpTypeOptions" :normalizer="normalizer" placeholder="请选择产品类型" />
        </div>
        </el-form-item>

      <el-form-item label="时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:article:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:article:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:article:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />

<!--      <el-table-column label="内容" align="center" prop="content" />-->
      <el-table-column label="多媒体类型" align="center" prop="dmtType"  v-if="dptlxshow" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dmt_type" :value="scope.row.dmtType"/>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="image" width="100" v-if="imageshow">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="视频" align="center" prop="video"  v-if="videoshow"
      />
<!--      <el-table-column label="模块类型" align="center" prop="mkType" />-->
      <el-table-column label="分类" align="center" prop="flTypeName" v-if="flTypeshow" />
      <el-table-column label="分类" align="center" prop="cpTypeName" v-if="cpTypeshow" />

      <el-table-column label="关键词" align="center" prop="keyword" v-if="contentshow" />
      <el-table-column label="发布时间" align="center" prop="pubDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pubDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布人" align="center" prop="pubPerson" />
      <el-table-column label="年度" align="center" prop="year"   v-if="isYearshow" />
      <el-table-column label="国家" align="center" prop="countryName"   v-if="isCountryshow" />
<!--      <el-table-column label="是否原创文章" align="center" prop="isYc"   v-if="isYcshow">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.sys_is_no" :value="scope.row.isYc"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="是否推荐" align="center" prop="isTj"   v-if="isTjshow">
<!--          <template slot-scope="scope">-->
<!--            <el-switch-->
<!--              v-model="scope.row.isTj"-->
<!--              active-value="1"-->
<!--              inactive-value="2"-->
<!--              @change="handleStatusChange(scope.row,'001')"-->
<!--            ></el-switch>-->
<!--          </template>-->
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_no" :value="scope.row.isTj"/>
        </template>
      </el-table-column>
      <el-table-column label="是否精选" align="center" prop="isRm"   v-if="isRmshow">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_no" :value="scope.row.isRm"/>
        </template>
      </el-table-column>
      <el-table-column label="是否首页业务指南" align="center" prop="isSyzd"   v-if="isSyzdshow">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_no" :value="scope.row.isSyzd"/>
        </template>
      </el-table-column>
      <el-table-column label="是否顶部banner" align="center" prop="isBanner"   v-if="isBannershow">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_no" :value="scope.row.isBanner"/>
        </template>
      </el-table-column>
      <el-table-column label="来源" align="center" prop="source"  v-if="contentshow"/>
      <el-table-column label="附件文件" align="center" prop="fjFile"   v-if="mkType=='002'" />
      <el-table-column label="操作"  fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:article:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:article:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文章对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="分类" prop="flType"  v-if="flTypeshow">
          <el-select v-model="form.flType" placeholder="请选择分类">
            <el-option
              v-for="dict in articleTypeOptions"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品类型" prop="cpType"  v-if="cpTypeshow">
          <treeselect v-model="form.cpType" :options="cpTypeOptions" :normalizer="normalizer" placeholder="请选择产品类型" />
        </el-form-item>

        <el-form-item label="多媒体类型" prop="dmtType"  v-if="dptlxshow" >
          <el-select v-model="form.dmtType" placeholder="请选择">
          <el-option
            v-for="dict in dict.type.dmt_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片" prop="image"  v-if="imageFormshow">
          <image-upload v-model="form.image" :limit="1"/>
        </el-form-item>
        <el-form-item label="视频" prop="video"  v-if="videoFormshow" >
          <file-upload v-model="form.video" :file-type="['mp4']"  :file-size="100"  :limit="1"/>
        </el-form-item>
        <el-form-item label="内容" v-if="contentshow">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="关键词" prop="keyword" v-if="contentshow">
          <el-input v-model="form.keyword" placeholder="请输入关键词" />
        </el-form-item>
        <el-form-item label="摘要内容" v-if="contentshow">
          <el-input  type="textarea" v-model="form.zyContent" placeholder="" />
        </el-form-item>
        <el-form-item label="发布人" prop="pubPerson">
          <el-input v-model="form.pubPerson" placeholder="请输入发布人" />
        </el-form-item>
        <el-form-item label="发布时间" prop="pubDate">
          <el-date-picker clearable
                          v-model="form.pubDate"
                          type="date"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="来源" prop="source" v-if="contentshow">
          <el-input v-model="form.source" placeholder="请输入来源" />
        </el-form-item>
        <el-form-item label="国家" prop="country" v-if="isCountryshow">
          <el-select v-model="form.country" placeholder="请选择">
            <el-option
              v-for="dict in countryTypeOptions"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="是否原创文章" prop="isYc" v-if="isYcshow">-->
<!--          <el-select v-model="form.isYc" placeholder="请选择">-->
<!--            <el-option-->
<!--              v-for="dict in dict.type.sys_is_no"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--            ></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->

        <el-form-item label="是否是首页业务指南" prop="isSyzd" v-if="isSyzdshow">
          <el-select v-model="form.isSyzd" placeholder="请选择">
            <el-option
              v-for="dict in dict.type.sys_is_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="是否顶部Banner" prop="isBanner" v-if="isBannershow">
          <el-select v-model="form.isBanner" placeholder="请选择是否顶部Banner">
            <el-option
              v-for="dict in dict.type.sys_is_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否推荐文章" prop="isTj" v-if="isTjshow">
          <el-select v-model="form.isTj" placeholder="请选择是否推荐文章">
            <el-option
              v-for="dict in dict.type.sys_is_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否精选文章" prop="isRm" v-if="isRmshow">
          <el-select v-model="form.isRm" placeholder="请选择">
            <el-option
              v-for="dict in dict.type.sys_is_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年度动态" prop="nddtType" v-if="form.flType=='1876785365296001025'">
          <el-select v-model="form.nddtType" placeholder="请选择">
            <el-option
              v-for="dict in ndarticleOptions"
              :key="dict.id"
              :label="dict.year"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="附件文件" prop="fjFile" v-if="isFjshow">
          <file-upload v-model="form.fjFile" :file-type=getFiletype :file-size="300"  :limit="1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listArticle, getArticle, delArticle, addArticle, updateArticle } from "@/api/business/article";
import { alllistType } from '@/api/business/articletype'
import  {allNdarticle} from '@/api/business/ndarticle'
import moment from 'moment';
import { alllistcountryTypeType } from '@/api/business/countrytype'
import { listCpType } from '@/api/business/articlecptype'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { changeUserStatus } from '@/api/system/user'
export default {
  name: "Article",
  dicts: ['sys_is_no','dmt_type'],
  components: { Treeselect },
  data() {
    return {

      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章表格数据
      articleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        source: null,
        content: null,
        image: null,
        video: null,
        mkType: null,
        flType: null,
        keyword: null,
        pubDate: null,
        pubPerson: null,
        country: null,
        isYc: null,
        fjFile: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      },
      articleTypeOptions:[],
      countryTypeOptions:[],
      mkType:"",
      cpTypeOptions:[],
      ndarticleOptions:[]
    };
  },
  created() {
    // 获取路由参数
    this.queryParams.mkType = this.$route.query.mktype;
    this.mkType=this.queryParams.mkType
    this.getList();
    this.getArticleType();
    this.getcountryType();
    this.getCpTypeData();
    this.getNdarticleData();
  },
  computed: {
    flTypeshow() {
      // 文章类型
      if(this.mkType=='001'  ||  this.mkType=='002' || this.mkType=='003' ){
        return true;
      }else{
        return  false
      }
    },
    imageshow() {
      // 图片显示
      if(this.mkType=='001'  ||  this.mkType=='002' || this.mkType=='003' ){
        return true;
      }else{
        return  false
      }
    },
    cpTypeshow() {
      if(this.mkType=='004'){
        return true;
      }else{
        return  false
      }
    },
    // 状态数据字典
    videoshow() {
      // 视频显示
      if(this.mkType=='003'){
        return true;
      }else{
        return  false
      }
    },
    videoFormshow() {
      // 视频显示
      if(this.mkType=='003'  &&  this.form.dmtType=='2'){
        return true;
      }else{
        return  false
      }
    },
    imageFormshow() {
      // 视频显示
      if((this.mkType=='001'  ||  this.mkType=='002' )|| (this.mkType=='003' )){
        return true;
      }else{
        return  false
      }
    },


    dptlxshow() {
      // 多媒体类型  视频或图片
      if(this.mkType=='003'){
        return true;
      }else{
        return  false
      }
    },
    isYcshow() {
      // 是否原创
      if(this.mkType=='002'){
        return true;
      }else{
        return  false
      }
    },
    isFjshow() {
      // 附件显示
      if(this.mkType=='002' || this.mkType=='001'   ){
        return true;
      }else{
        return  false
      }
    },


    getFiletype(){
      if(this.mkType=='001'){
        return ['pdf']
      }  if(this.mkType=='002'){
        return ['doc','docx','pdf']
      }else{
        return ['doc','docx','pdf']
      }

    },

    isTjshow() {
      // 是否推荐
      if(this.mkType=='001'){
        return true;
      }else{
        return  false
      }
    },
    isRmshow() {
      // 是否精选
      if(this.mkType=='001'){
        return true;
      }else{
        return  false
      }
    },
    isYearshow(){
      if(this.mkType=='001'){
        return true;
      }else{
        return  false
      }
    },
    isCountryshow() {
      // 是否国家
      if(this.mkType=='002'){
        return true;
      }else{
        return  false
      }
    },
    isBannershow() {
      // 是否banner
      if(this.mkType=='001'  ||   this.mkType=='002' ){
        return true;
      }else{
        return  false
      }
    },

    isSyzdshow() {
      // 是否首页业务指南
      if(this.mkType=='001'){
        return true;
      }else{
        return  false
      }
    },



    contentshow() {
      // 内容显示  安全动态      专题研究   产品展示
      if(this.mkType=='001'  ||  this.mkType=='002'     ||  this.mkType=='004'   ){
        return true;
      }else{
        return  false
      }
    },


  },
  methods: {
    getNdarticleData(){
      allNdarticle().then(response => {
        this.ndarticleOptions=response.data
      })

    },
    handleStatusChange(row,type) {
      var  that=this;
      getArticle(row.id).then(response => {
        that.form = response.data;
        // 热门
        if(type=='001'){
          that.$modal.confirm('确认要设置吗？').then(function() {
            if(type=='001'){
              that.form.isTj = row.isTj;
            }
            if(type=='002'){
              that.form.isRm = row.isRm;
            }
            return updateArticle(that.form);
          }).then(() => {
            that.$modal.msgSuccess(text + "成功");
          }).catch(function() {
            row.isTj = row.isTj === "1" ? "2" : "1";
          });
        }
        // if(type=='002'){
        //   that.$modal.confirm('确认要设置吗').then(function() {
        //     that.form.isRm = row.isRm;
        //     return updateArticle(that.form);
        //   }).then(() => {
        //     that.$modal.msgSuccess(text + "成功");
        //   }).catch(function() {
        //     row.isRm = row.isRm === "1" ? "2" : "1";
        //   });
        // }

      });




    },
    getCpTypeData() {
      listCpType().then(response => {
        this.cpTypeOptions =  this.handleTree(response.data, 'id')
      });
    },
    handleNodeClickquery(data) {

    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },
    handleNodeClick(data) {
      if (data.parentId == '0') {
        this.$modal.msgError('请选择二级节点')
      }
      return
    },
    getcountryType(){
      alllistcountryTypeType().then(response => {
        this.countryTypeOptions = response.data;
      });
    },
    getArticleType(){
      var data={
        mkType:this.mkType
      }
      alllistType(data).then(response => {
        this.articleTypeOptions = response.data;
      });
    },
    /** 查询文章列表 */
    getList() {
      this.loading = true;
      listArticle(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.articleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        source: null,
        content: null,
        image: null,
        video: null,
        mkType: null,
        flType: null,
        keyword: null,
        pubDate: null,
        pubPerson: null,
        country: null,
        isYc: null,
        fjFile: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.mkType = this.queryParams.mkType;
      // 发布时间 pubDate  默认当前时间
      this.form.pubDate = moment(new Date()).format('YYYY-MM-DD HH:mm:ss');

      this.open = true;
      this.title = "添加文章";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getArticle(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文章";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateArticle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArticle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除文章编号为"' + ids + '"的数据项？').then(function() {
        return delArticle(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/article/export', {
        ...this.queryParams
      }, `article_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
